/* $Id: CreateFilterPanel.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.client.ui.grids;


import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;


public class CreateFilterPanel extends Composite
{
	interface MyUiBinder extends UiBinder<VerticalPanel, CreateFilterPanel> {}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	@UiField
	TabLayoutPanel tabs;
	
	@UiField
	TextBox tbPrefix;
	
	@UiField
	TextBox tbSuffix;
	
	@UiField
	TextBox tbRegexpJava;
	
	@UiField
	TextBox tbRegexpJavaScript;
	
	@UiField
	TextBox tbIntegerCompare;
	
	@UiField
	RadioButton rbCompare;
	
	@UiField
	ListBox lbOperator;
	
	@UiField
	RadioButton rbBetween;
	
	@UiField
	TextBox tbIntegerBetween1;
	
	@UiField
	TextBox tbIntegerBetween2;
	
	@UiField
	ResizeLayoutPanel resizeLayoutPanel;
	
	@UiField
	TextBox tbTest;
	
	@UiField
	Button btnTest;
	
	@UiField
	Image imgTest;
	
	public CreateFilterPanel()
	{
		initWidget(uiBinder.createAndBindUi(this));
		
		btnTest.addClickHandler(new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent event)
			{
				Filter filter = getFilter();
				String testString = tbTest.getText();
				if (filter.matches(testString))
				{
					imgTest.setUrl("/images/icons/dot_green.png");
					imgTest.setTitle("String matches filter");
				}
				else
				{
					imgTest.setUrl("/images/icons/dot_red.png");
					imgTest.setTitle("String doesn't match filter");
				}
				
			}
		});
		
		// TODO: move this operations to ui.xml file
		String[] operations = new String[] {"=", "!=", "<", ">", "<=", ">="};
		for (String op: operations)
			lbOperator.addItem(op, op);
	}
	
	public Filter getFilter()
	{
		int tabIndex = tabs.getSelectedIndex();
		Filter filter = null;
		if (tabIndex == 0)
		{
			String prefix = tbPrefix.getText();
			filter = new PrefixFilter(prefix);
		}
		else if (tabIndex == 1)
		{
			String suffix = tbSuffix.getText();
			filter = new SuffixFilter(suffix);
		}
		else if (tabIndex == 2)
		{
			String regexp = tbRegexpJava.getText();
			filter = new JavaRegexpFilter(regexp);
		}
		else if (tabIndex == 3)
		{
			String regexp = tbRegexpJavaScript.getText();
			filter = new JavascriptRegexpFilter(regexp);
		}
		else if (tabIndex == 4)
		{
			if (rbCompare.getValue())
			{
				String valueString = tbIntegerCompare.getText();
				String operationString = lbOperator.getItemText(lbOperator.getSelectedIndex());
				filter = new IntegerFilterCompare(valueString, operationString);
			}
			else if (rbBetween.getValue())
			{
				String leftBoundString = tbIntegerBetween1.getText();
				String rightBoundString = tbIntegerBetween2.getText();
				filter = new IntegerFilterRange(leftBoundString, rightBoundString);
			}
		}
		return filter;
	}
}
